#include "Banner.h"
#include "ResourcesManager.h"
#include "ConfigManager.h"

Banner::Banner()
{
    sizeForeground = {646, 215};
    sizeBackground = {1282, 209};

    timerDisplay.SetOneShot(true);
    timerDisplay.SetWaitTime(5);
    timerDisplay.SetOnTimeout([&]() { isEndDisplay = true; });
}

void Banner::SetCenterPosition(const Vector2 &pos)
{
    posCenter = pos;
}

void Banner::OnUpdate(double delta)
{
    timerDisplay.OnUpdate(delta);

    const ResourcesManager::TexturePool &texturePool = ResourcesManager::Instance().texturePool;
    const ConfigManager &instance = ConfigManager::Instance();

    texForeground = texturePool.find(instance.is_game_win ? ResID::Tex_UIWinText : ResID::Tex_UILossText)->second;
    texBackground = texturePool.find(ResID::Tex_UIGameOverBar)->second;
}

void Banner::OnRender(SDL_Renderer *renderer)
{
    static SDL_Rect rect_dst;

    rect_dst.x = (int) (posCenter.x - sizeBackground.x / 2);
    rect_dst.y = (int) (posCenter.y - sizeBackground.y / 2);
    rect_dst.w = (int) sizeBackground.x, rect_dst.h = (int) sizeBackground.y;
    SDL_RenderCopy(renderer, texBackground, nullptr, &rect_dst);

    rect_dst.x = (int) (posCenter.x - sizeForeground.x / 2);
    rect_dst.y = (int) (posCenter.y - sizeForeground.y / 2);
    rect_dst.w = (int) sizeForeground.x, rect_dst.h = (int) sizeForeground.y;
    SDL_RenderCopy(renderer, texForeground, nullptr, &rect_dst);
}

bool Banner::CheckEndDisplay() const
{
    return isEndDisplay;
}